#include<stdio.h>
#include<string.h>
#include<sys/wait.h>
#include<stdlib.h>
#include<sys/types.h>
#include<unistd.h>
#define NUM 128
#define CMD_NUM 64
int main()
{
  char command[NUM] = {0};//保存命令行输入的内容
  char* argv[CMD_NUM] = {NULL};//保存命令行参数
  while(1)
  {
    command[0] = '\0';
    printf("[whos@hostname dir]$ ");
    fflush(stdout);
    
    ssize_t n = read(0,command,sizeof(command));
    command[n-1] = '\0';
    const char* sep = " ";
    argv[0] = strtok(command,sep);
    int index = 1;
    while(argv[index++] = strtok(NULL,sep)) ;
    
    if(strcmp(argv[0],"cd") == 0)
    {
        if(argv[1] != NULL)
          chdir(argv[1]);
        continue;
    }
    pid_t id = fork();
    if(id == 0)
    {
        execvp(argv[0],argv);
        exit(2);
    }
    //parent
    int status = 0;
    int res = waitpid(id,&status,0);
    if(res > 0)
    {
      printf("exit code:%d signale:%d\n",(status>>8)&0xff,status&0x7f);
    }
  }
  return 0;
}
